package com.crayon2f.test.bit;

import org.junit.Test;

/**
 * Created by feifan.gou@gmail.com on 2020/12/24 16:19.
 */
public class BiTOpt {

    @Test
    public void test() {

        int a = -1;

        System.out.println(a >>> 2);

        System.out.println(Integer.toBinaryString(a));
        System.out.println(Integer.toBinaryString(1));
        System.out.println(Integer.toBinaryString(2));
        System.out.println(~1);

        int bitmask = 0x000F;
        int val = 0x2222;
        // prints "2"
        System.out.println(val & bitmask);

        System.out.println(-1 >>> 1);
    }

    @Test
    public void testBit() {

//        System.out.println(Integer.toBinaryString(5));
//        System.out.println(~5);
//        System.out.println(Integer.toBinaryString(~5));
//        System.out.println(Integer.toBinaryString(-5));
//        long b = -5;
//        System.out.println(Integer.toBinaryString(b));
//        System.out.println(Long.toBinaryString(b));

//        11111111111111111111111111111011
//        System.out.println(-5>>>2); //1111111111111111111111111111
//        System.out.println(-100 / 4 );
//        System.out.println(-100 >> 2 );
//        System.out.println(Integer.toBinaryString(-10));
        //
        //0000000000000000000000000001010
        //1111111111111111111111111110101
        //1111111111111111111111111110110 (-10的二进制)
        //0011111111111111111111111111101 (-10 无符号右移两位)
        System.out.println(Integer.toBinaryString(-10));
        System.out.println(Integer.toBinaryString(1073741821));
        System.out.println(-10 >>> 2);
//        String string = "0011111111111111111111111111101";
//        System.out.println(Integer.parseInt(string, 2));
    }


    @Test
    public void testOrAnd() {

        int i = 3; //0011
        int j = 2; //0010

        System.out.println(i | j);
        System.out.println(i & j);
        System.out.println(i ^ j);

        // 0110 6
        // 1000 8
        // 1110
        System.out.println(6 ^ 8);
    }
}
